//
//  FHStatusOriginalView.swift
//  WeiBo
//
//  Created by qfh on 16/7/13.
//  Copyright © 2016年 qiufuhe. All rights reserved.
//

import UIKit
import SnapKit

class FHStatusOriginalView: UIView {
    
    var contentBottom: Constraint?
    
    
    var statusViewMoel: FHStatusViewModel? {
    
        didSet {
            
            if let name = statusViewMoel?.status?.user?.screen_name, let img_url = statusViewMoel?.status?.user?.profile_image_url {
                
                
                screenNameLabel.text = name
                
                headImageView.sd_setImageWithURL(NSURL(string: img_url), placeholderImage: UIImage(named: "avatar_default_big"))
                
                
                
            }
            
            
            
            timeLabel.text = statusViewMoel?.timeStr
            
            sourceLabel.text = statusViewMoel?.sourceContent
            
            
            contentLabel.text = statusViewMoel?.status?.text
        
            //设置vip 等级
            verifiedTypeImageView.image = statusViewMoel?.verifiedImage
            // 设置认证 等级
            mbrankImageView.image = statusViewMoel?.mbrankImage
            
            
            
            // 设置 pictrue 
            
            contentBottom?.uninstall()
            
            if let pictrues = statusViewMoel?.status?.pic_urls where pictrues.count > 0 {
            
                self.snp_updateConstraints { (make) -> Void in
                    
                    
                    self.contentBottom =  make.bottom.equalTo(pictureImage.snp_bottom).offset(HomeCellMargin).constraint
                    
                }
                
                pictureImage.pictures = pictrues
                
                pictureImage.hidden = false
            
            
            } else {
                
                self.snp_updateConstraints { (make) -> Void in
                    
                    
                    self.contentBottom =  make.bottom.equalTo(contentLabel.snp_bottom).offset(HomeCellMargin).constraint
                    
                }
            
                pictureImage.hidden = true
            
            
            }
            
            
            
        
        }
    
    
    }
    
    
    // 图片视图
    
    lazy var pictureImage: FHStatusPictrueView = FHStatusPictrueView()
    
    

    // 微博头像
    private lazy var headImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    // 认证等级图片
    private lazy var verifiedTypeImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    // 昵称
    
    private lazy var screenNameLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), size: 15)
    
    //会有等级
    
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    //时间
    
    private lazy var timeLabel: UILabel = UILabel(textColor: UIColor.orangeColor(), size: 12)
    
    // 来源
    
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.lightGrayColor(), size: 15)
    
    // 微博内容
    
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), size: 15)
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUi()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUpUi() {
        
        backgroundColor = UIColor.whiteColor()
        
        pictureImage.backgroundColor = backgroundColor
        
        addSubview(pictureImage)
    
        addSubview(headImageView)
        
        addSubview(verifiedTypeImageView)
        
        addSubview(screenNameLabel)
        
        addSubview(mbrankImageView)
        
        addSubview(timeLabel)
        
        addSubview(sourceLabel)
        
        addSubview(contentLabel)
        
        
        headImageView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self).offset(HomeCellMargin)
            
            make.leading.equalTo(self).offset(HomeCellMargin)
            
            make.size.equalTo(CGSize(width: 35, height: 35))
            
        }
    
        verifiedTypeImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(headImageView.snp_trailing)
            
            make.centerY.equalTo(headImageView.snp_bottom)
            
        }
        
        screenNameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImageView.snp_trailing).offset(HomeCellMargin)
            
            make.top.equalTo(headImageView)
        }
        
        mbrankImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(screenNameLabel)
            
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(HomeCellMargin)
            
            
        }


        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImageView.snp_trailing).offset(HomeCellMargin)
            
            make.top.equalTo(screenNameLabel.snp_bottom).offset(HomeCellMargin / 2)
            
            
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(HomeCellMargin)
            
            make.centerY.equalTo(timeLabel)
            
            
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(HomeCellMargin)
            
            make.leading.equalTo(headImageView)
            
            make.trailing.equalTo(self).offset(-HomeCellMargin)
        }
        
        pictureImage.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            
            make.top.equalTo(contentLabel.snp_bottom).offset(HomeCellMargin)
            
        }
        
        
        self.snp_makeConstraints { (make) -> Void in
            
            
           self.contentBottom =  make.bottom.equalTo(pictureImage.snp_bottom).offset(HomeCellMargin).constraint
            
        }
    
        
    }
    
    
    

}
